/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-08-04
 * Time: 15:56
 */
class Solution4 {
    public String countAndSay(int n) {
        String ret = "1";

        for(int i = 1; i < n; i++) {
            // 保存结果
            StringBuilder str = new StringBuilder();
            int len = ret.length();
            for(int left = 0, right = 0; right < len;) {
                while(right < len && ret.charAt(left) == ret.charAt(right)) {
                    right++;
                }
                str.append(Integer.toString(right - left));
                str.append(ret.charAt(left));
                left = right;
            }
            // 更新ret
            ret = str.toString();
        }
        return ret;
    }
}
